package com.dg.river.module.web;

import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.dg.river.core.util.L;
import com.dg.river.core.util.TextUtil;
import com.dg.river.core.util.WebViewUtils;
import com.dg.river.databinding.ActivityWebLinkBinding;
import com.dg.river.module.base.BaseActivity;

import org.greenrobot.eventbus.EventBus;

/**
 * @作者：HuYong
 * @时间：2023/4/12 16:19
 * @描述：网页
 * @xml：activity_web_link.xml
 * @修改：
 */
public class WebLinkActivity extends BaseActivity {

    private ActivityWebLinkBinding binding;

    private String url;

    private String title;

    // URL展示路径
    public static String URL = "url";
    // 标题内容
    public static String TITLE = "title";

    public static void start(Context mContext, String url, String title) {
        Intent intent = new Intent(mContext, WebLinkActivity.class);
        intent.putExtra(URL, url);
        intent.putExtra(TITLE, title);
        mContext.startActivity(intent);
    }

    @NonNull
    @Override
    public View initLayout() {
        binding = ActivityWebLinkBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    @Override
    protected void initGetIntent() {
        super.initGetIntent();
        url = getIntent().getStringExtra(URL);
        title = getIntent().getStringExtra(TITLE);
    }

    @Override
    public void initView() {
        binding.includeTitle.tvTitle.setText(TextUtil.isEmptyConvert(title));
    }

    @Override
    public boolean viewAndData() {
        return true;
    }

    @Override
    public void initData() {
        ProgressBar progressBar = binding.progressBar;
        WebViewUtils.webSetting(mContext, binding.webView, true);
        WebViewUtils.webShowProgressBar(progressBar, binding.webView);

        L.d("===== 打开的H5链接为 =====");
        binding.webView.loadUrl(url);
        L.d("===== " + url + " =====");
    }

    @Override
    public void initListener() {
        binding.includeTitle.mIvBack.setOnClickListener(v -> {
            WebViewUtils.webCanGoBack(this, binding.webView);
        });
    }

    @Override
    public void showErrorMessage(@Nullable String message) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            WebViewUtils.webCanGoBack(this, binding.webView);
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
